use std::collections::HashMap;

use reqwest::header::{COOKIE, HeaderMap, USER_AGENT};

use crate::http::{Credential, generate_random_ua};

pub mod album;
pub mod api;
pub mod user;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BiliLoginCredential {
    sessdata: String,
    bili_jct: String,
    dede_user_id: String,
    buvid3: String,
}

impl Credential for BiliLoginCredential {
    fn get_cookies(&self) -> HashMap<String, String> {
        let mut hash_map = HashMap::new();
        hash_map.insert("SESSDATA".to_owned(), self.sessdata.to_owned());
        hash_map.insert("bili_jct".to_owned(), self.bili_jct.to_owned());
        hash_map.insert("DedeUserID".to_owned(), self.dede_user_id.to_owned());
        hash_map.insert("buvid3".to_owned(), self.buvid3.to_owned());
        hash_map
    }

    fn get_cookie_string(&self) -> String {
        format!(
            "SESSDATA={};bili_jct={};DedeUserID={};buvid3={}",
            self.sessdata, self.bili_jct, self.dede_user_id, self.buvid3,
        )
    }

    fn get_headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert(USER_AGENT, generate_random_ua().parse().unwrap());
        headers.insert(COOKIE, self.get_cookie_string().parse().unwrap());
        headers
    }
}

impl Default for BiliLoginCredential {
    fn default() -> Self {
        Self {
            sessdata: "".to_owned(),
            bili_jct: "".to_owned(),
            dede_user_id: "".to_owned(),
            buvid3: "".to_owned(),
        }
    }
}

impl BiliLoginCredential {
    pub fn new(sessdata: &str, bili_jct: &str, dui: &str, buvid3: &str) -> Self {
        Self {
            sessdata: sessdata.to_string(),
            bili_jct: bili_jct.to_string(),
            dede_user_id: dui.to_string(),
            buvid3: buvid3.to_string(),
        }
    }
}
